//
//  PaiController.m
//  Pai9
//
//  Created by Zhou Tao on 10-4-19.
//  Copyright 2010 Free. All rights reserved.
//

#import "PaiController.h"

static int cards[]={1,1,2,2,3,3,5,5,6,6,7,7,9,9,10,10,11,11,12,12,13,15,16,17,18,19,25,27,28,29};

@implementation PaiController
@class Player;
@synthesize card;
@synthesize ai1;
@synthesize ai2;
@synthesize player;

-(id) init
{
	card = (int*)malloc(sizeof(int) * MaxCards);
	bcflag = (int*)malloc(sizeof(int) * BlackCards);
	
	ai1 = [[Player alloc] init];	
	ai2 = [[Player alloc] init];
	player = [[Player alloc] init];
						  
	[super init];
	return self;
}
-(void) reStart
{
	int i;
	int idx;
	for (i=0; i<MaxCards; i++) 
	{
		card[i] = 0;
	}
	memset(bcflag, 0, sizeof(int)*BlackCards);
	
	srand((int)time(0));
	for(i=0;i<MaxCards;i++)
	{
		idx = rand()  % MaxCards;
		while (card[idx]>0) 
		{
			idx++;
			if(idx >= MaxCards)
				idx = 0;
		}
		card[idx] = cards[i];
	}
	
	// 
	ai1.card = [self getRand10:0];
	player.card = [self getRand10:1];
	ai2.card = [self getRand10:2];
	
	[player sortCard];
	[ai1 sortCard];
	[ai2 sortCard];
	
	[ai1 logOut];
	[player logOut];
	[ai2 logOut];
}
-(int *) getRand10:(int) idx
{
	
	return card+10*idx;
}

-(void) logOut
{
	for (int i=0; i<MaxCards; i++) 
	{
		printf("Idx %2d : %d\n",i,card[i]);
	}
}




@end
